package com.zmn.plat.admin.controller.orderconfig.track.reason;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.plat.model.entity.orderconfig.track.OrderTrack;
import com.zmn.plat.model.entity.orderconfig.track.reason.OrderTrackReason;
import com.zmn.plat.model.entity.orderconfig.track.reason.OrderTrackReasonQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.plat.model.entity.orderconfig.track.category.OrderTrackCategory;
import com.zmn.plat.model.entity.orderconfig.track.category.OrderTrackCategoryQuery;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description ：跟单分类
 * @since 2020/11/17 16:49
 * @modifier
 **/
@Controller
@RequestMapping("/orderconfig/trackReason/")
@Slf4j
public class OrderTrackReasonListController extends OrderTrackReasonBaseController {

    /**
     * 描述: 跟单分类列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     * @author tanbiao
     * @since 2020/11/17
     */
    @SuppressWarnings("Duplicates")
    @RequestMapping(value = "list")
    @RequiresPermissions("/orderconfig/trackReason/list.action")
    public ModelAndView list(@ModelAttribute OrderTrackReasonQuery query) {
        ModelAndView mav = new ModelAndView("orderconfig/track/reason/list");
        List<OrderTrackReason> cancelCategories = orderTrackReasonService.listPageByQuery(query);
        mav.addObject("items", cancelCategories);
        mav.addObject("vo", query);
        return mav;
    }

    /**
     * 功能描述 ：详情页面
     *
     * @param categId
     * @return org.springframework.web.servlet.ModelAndView
     * @author tanbiao
     * @since 2020/11/17
     * @modifier
     */
    @RequestMapping(value = "detail")
    @RequiresPermissions("/orderconfig/trackReason/detail.action")
    public ModelAndView detail(@RequestParam(value = "categId", defaultValue = "0") Integer categId) {
        ModelAndView mav = new ModelAndView("orderconfig/track/reason/detail");
        OrderTrackReason reason = null;
        if (categId == null || categId == 0) {
            reason = new OrderTrackReason();
        } else {
            reason = Optional.ofNullable(orderTrackReasonService.findByKey(categId)).orElse(new OrderTrackReason());
        }
        if (reason.getSort() == null) {
            Integer sort = orderTrackReasonService.findMaxSort();
            sort = sort == null ? 1 : sort + 1;
            reason.setSort(sort);
        }
        mav.addObject("reason", reason);
        return mav;
    }

    @RequestMapping(value = "listVt")
    @ResponseBody
    public ResponseDTO<List<VtDTO>> listDept(@RequestParam(value = "trackId",defaultValue = "0")Integer trackId) {
        List<VtDTO> list = new ArrayList<>();
        // 二级缘由多选
        List<OrderTrackReason> reasonList = orderTrackReasonService.listByQuery(new OrderTrackReasonQuery());
        if (trackId == null || trackId == 0) {
            reasonList = reasonList.stream().filter(e -> Objects.equals(e.getStatus(), GlobalConsts.YES)).collect(Collectors.toList());
        }else{
            OrderTrack track = orderTrackService.findByKey(trackId);
            if(Objects.nonNull(track)){
                String trackReason = track.getTrackReason();
                reasonList = reasonList.stream().filter(e -> (Objects.equals(e.getStatus(), GlobalConsts.YES) || (StringUtil.isNotBlank(trackReason) && trackReason.contains(e.getReasonId() + ""))))
                        .collect(Collectors.toList());
            }
        }
        if(!CollectionUtils.isEmpty(reasonList)){
           list = reasonList.stream().map(e->new VtDTO(e.getReasonId(),e.getReason())).collect(Collectors.toList());
        }
        return ResponseDTO.success(list);
    }
}
